const express = require('express')

const router = express.Router()
const mysql = require('../db')

module.exports = router


// 获取指定类型影片(更多影片)
router.get('/show/category/:videoClass', async (req, res) => {
    // router.get('/show/category/:videClass/addres/:nation/year/:startTime', async (req, res) => {
    // console.log(req.params);
    const { videoClass } = req.params
    // const { size, page } = req.query
    let size = req.query.size || 20
    let page = req.query.page || 1

    const sql = `select * from movielist where videoClass='${videoClass}' limit ${(page - 1) * size},${size}`
    const movielist = await mysql.query(sql)
    res.send({
        code: 200,
        movielist
    })
})
// 电影类型和生产地匹配
router.get('/show/category/:videoClass/address/:address', async (req, res) => {
    // router.get('/show/category/:videClass/addres/:nation/year/:startTime', async (req, res) => {
    // console.log(req.params);
    const { videoClass, address } = req.params
    // const { size, page } = req.query
    let size = req.query.size || 10
    let page = req.query.page || 1
    const sql = `select * from movielist where videoClass='${videoClass}' and address='${address}' limit ${(page - 1) * size},${size}`
    const movielist = await mysql.query(sql)
    res.send({
        code: 200,
        movielist
    })
})
// 电影类型和生产地和年份匹配
router.get('/show/category/:videoClass/address/:address/starTime/:starTime', async (req, res) => {
    // router.get('/show/category/:videClass/addres/:nation/year/:startTime', async (req, res) => {
    // console.log(req.params);
    const { videoClass, address, starTime } = req.params
    // const { size, page } = req.query
    let size = req.query.size || 10
    let page = req.query.page || 1
    const sql = `select * from movielist where videoClass='${videoClass}' and address='${address}' and startTime='${starTime}' limit ${(page - 1) * size},${size}`
    const movielist = await mysql.query(sql)
    res.send({
        code: 200,
        movielist
    })
})

// 电影类型和年份匹配
router.get('/show/category/:videoClass/starTime/:starTime', async (req, res) => {

    const { videoClass, starTime } = req.params
    // console.log(starTime);
    // const { size, page } = req.query
    let size = req.query.size || 10
    let page = req.query.page || 1
    const sql = `select * from movielist where videoClass='${videoClass}' and startTime='${starTime}' limit ${(page - 1) * size},${size}`
    const movielist = await mysql.query(sql)
    res.send({
        code: 200,
        movielist
    })
})
// 搜索页面 不传参默认 第一页10条 传kw模糊搜索  page，size指定页码和条数 默认第一页10条数据
router.get('/search', async (req, res) => {
    let page = req.query.page || 1
    let size = req.query.size || 10
    if (req.query.kw) {
        let kw = req.query.kw
        const sql = `select * from movielist where title like '%${kw}%' limit ${(page - 1) * size},${size}`
        const movielist = await mysql.query(sql)
        res.send({
            code: 200,
            movielist
        })
    }
    else {
        // let page = req.query.page || 1
        // let size = req.query.size || 10
        const sql = `select * from movielist limit ${(page - 1) * size},${size}`
        const movielist = await mysql.query(sql)
        res.send({
            code: 200,
            movielist
        })
    }
})


